package com.book.modules.core.log.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.book.common.api.entity.CurrentUser;
import com.book.common.api.helper.TransformHelper;
import com.book.common.exception.BaseException;
import com.book.common.result.Failed;
import com.book.common.result.PageInfo;
import com.book.common.result.RestResponse;
import com.book.common.result.Success;
import com.book.common.api.core.RESTfulMethod;
import com.book.modules.core.controller.BaseController;
import com.book.modules.core.log.dto.LogInfoDto;
import com.book.modules.core.log.entity.LogInfo;
import com.book.modules.core.log.service.ILogInfoService;
import com.book.modules.core.log.vo.LogInfoVo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Description：
 * @Author： leo.xiong
 * @CreateDate： 2020-04-14
 * @Email： leo.xiong@suyun360.com
 * @Version:
 */
@Slf4j
@Validated
@RestController
@Api(value = "操作日志信息")
@RequestMapping(value = "${adminPath}/logInfo")
public class LogInfoController extends BaseController {

    @Autowired
    public ILogInfoService logInfoService;

    /**
     * 根据ID获取操作日志信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ResponseBody
    @ApiOperation(value = "根据ID获取操作日志信息", httpMethod = RESTfulMethod.GET, response = Success.class)
    public RestResponse get(@NotBlank(message = "{required}") @PathVariable String id, @ApiIgnore CurrentUser currentUser) {
        try {
            if (StringUtils.isEmpty(id)) {
                return new Failed("500", i18nHelper.get("获取操作日志失败"));
            }
            return new Success(this.logInfoService.getById(id));
        } catch (Exception e) {
            log.error("查询操作日志失败", e);
            return new Failed("500", i18nHelper.get(e.getMessage()));
        }
    }

    /**
     * 查询操作日志信息
     *
     * @param logInfoDto
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    @ApiOperation(value = "查询操作日志信息", httpMethod = RESTfulMethod.GET, response = Success.class)
    public RestResponse list(@RequestParam(value = "logInfoDto") LogInfoDto logInfoDto, @ApiIgnore CurrentUser currentUser) {
        try {
            LogInfo logInfo = TransformHelper.transform(logInfoDto, LogInfo.class);
            List<LogInfo> logInfoList = this.logInfoService.findAllList(logInfo);
            return new Success(TransformHelper.transform(logInfoList, LogInfoVo.class));
        } catch (Exception e) {
            log.error("查询操作日志失败", e);
            return new Failed("查询操作日志失败", e.getMessage());
        }
    }

    /**
     * 查询操作日志信息
     *
     * @param logInfoDto
     * @param pageInfo
     * @return
     */
    @PostMapping("page/list")
    @ResponseBody
    @ApiOperation(value = "查询操作日志分页信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse pageList(@RequestParam(value = "logInfo") LogInfoDto logInfoDto, PageInfo<LogInfo> pageInfo, @ApiIgnore CurrentUser currentUser) {
        try {
            LogInfo logInfo = TransformHelper.transform(logInfoDto, LogInfo.class);
            PageInfo<LogInfo> logInfoPageInfo = this.logInfoService.findPageList(logInfo, pageInfo);
            return new Success(TransformHelper.transformPage(logInfoPageInfo, LogInfoVo.class));
        } catch (Exception e) {
            log.error("分页查询操作日志失败", e);
            return new Failed("分页查询操作日志失败", e.getMessage());
        }
    }

    /**
     * 新增操作日志信息
     *
     * @param logInfoDto
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    @ApiOperation(value = "保存操作日志信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse save(@Valid LogInfoDto logInfoDto, @ApiIgnore CurrentUser currentUser) {
        try {
            this.logInfoService.save(TransformHelper.transform(logInfoDto, LogInfo.class));
            return new Success("新增操作日志成功");
        } catch (Exception e) {
            log.error("新增操作日志失败", e);
            return new Failed("新增操作日志失败", e.getMessage());
        }
    }

    /**
     * 修改操作日志信息
     *
     * @param logInfoDto
     * @return
     */
    @PutMapping("update")
    @ResponseBody
    @ApiOperation(value = "修改操作日志信息", httpMethod = RESTfulMethod.PUT, response = Success.class)
    public RestResponse update(@Valid LogInfoDto logInfoDto, @ApiIgnore CurrentUser currentUser) {
        try {
            if (logInfoDto == null || logInfoDto.getId() == null) {
                return new Failed("修改操作日志失败修改操作日志信息为空");
            }
            this.logInfoService.update(Wrappers.update(TransformHelper.transform(logInfoDto, LogInfo.class)));
            return new Success("修改操作日志成功");
        } catch (Exception e) {
            log.error("修改操作日志失败", e);
            return new Failed("修改操作日志失败", e.getMessage());
        }
    }

    /**
     * 删除操作日志信息
     *
     * @param logInfoId
     * @return
     */
    @DeleteMapping("delete/{id}")
    @ResponseBody
    @ApiOperation(value = "根据ID删除操作日志信息", httpMethod = RESTfulMethod.DELETE, response = Success.class)
    public RestResponse delete(@NotBlank(message = "{required}") @PathVariable String logInfoId, @ApiIgnore CurrentUser currentUser) {
        try {
            this.logInfoService.removeByIds(Lists.newArrayList(logInfoId.split(StringPool.COMMA)));
            return new Success("删除操作日志成功");
        } catch (Exception e) {
            log.error("删除操作日志失败", e);
            return new Failed("删除操作日志失败", e.getMessage());
        }
    }

    /**
     * 导出操作日志信息
     *
     * @param logInfoDto
     * @return
     */
    @PostMapping("excel")
    @ResponseBody
    @ApiOperation(value = "导出操作日志信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public void export(@RequestParam(value = "logInfo") LogInfoDto logInfoDto, @ApiIgnore CurrentUser currentUser) throws BaseException {
        try {
            List<LogInfo> logInfoList = this.logInfoService.findAllList(TransformHelper.transform(logInfoDto, LogInfo.class));
        } catch (Exception e) {
            log.error("导出操作日志信息失败", e);
            throw new BaseException("导出操作日志信息失败");
        }
    }
}
